package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.DormitoryApply;
import com.amsp.oa.oademo.service.DormitoryApplyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/api/dormitory-apply")
public class DormitoryApplyController {

    @Autowired
    private DormitoryApplyService dormitoryApplyService;

    @GetMapping
    public ResponseEntity<IPage<DormitoryApply>> getApplyList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Page<DormitoryApply> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(dormitoryApplyService.getApplyList(pageParam, keyword, status));
    }

    @GetMapping("/my")
    public ResponseEntity<IPage<DormitoryApply>> getMyApplies(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam String applicant) {
        Page<DormitoryApply> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(dormitoryApplyService.getMyApplies(pageParam, applicant));
    }

    @GetMapping("/{id}")
    public ResponseEntity<DormitoryApply> getApply(@PathVariable Long id) {
        return ResponseEntity.ok(dormitoryApplyService.getById(id));
    }

    @GetMapping("/no/{applyNo}")
    public ResponseEntity<DormitoryApply> getApplyByNo(@PathVariable String applyNo) {
        return ResponseEntity.ok(dormitoryApplyService.getApplyByNo(applyNo));
    }

    @PostMapping("/submit")
    public ResponseEntity<DormitoryApply> submitApply(@RequestBody DormitoryApply apply) {
        return ResponseEntity.ok(dormitoryApplyService.submitApply(apply));
    }

    @PutMapping("/{id}")
    public ResponseEntity<DormitoryApply> updateApply(
            @PathVariable Long id,
            @RequestBody DormitoryApply apply) {
        apply.setId(id);
        return ResponseEntity.ok(dormitoryApplyService.updateApply(apply));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteApply(@PathVariable Long id) {
        dormitoryApplyService.deleteApply(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/approve")
    public ResponseEntity<DormitoryApply> approveApply(
            @PathVariable Long id,
            @RequestParam String approver,
            @RequestParam String approveRemark,
            @RequestParam boolean isApproved) {
        return ResponseEntity.ok(dormitoryApplyService.approveApply(id, approver, approveRemark, isApproved));
    }

    @PostMapping("/{id}/assign")
    public ResponseEntity<DormitoryApply> assignRoom(
            @PathVariable Long id,
            @RequestParam String building,
            @RequestParam String room,
            @RequestParam Date checkInTime,
            @RequestParam Date checkOutTime) {
        return ResponseEntity.ok(dormitoryApplyService.assignRoom(id, building, room, checkInTime, checkOutTime));
    }

    @PostMapping("/{id}/cancel")
    public ResponseEntity<DormitoryApply> cancelApply(@PathVariable Long id) {
        return ResponseEntity.ok(dormitoryApplyService.cancelApply(id));
    }
} 